package com.appspot.simplebookmarks.controller.bookmark;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;

import com.appspot.simplebookmarks.model.Bookmark;
import com.appspot.simplebookmarks.model.Collection;
import com.appspot.simplebookmarks.service.BookmarkService;
import com.appspot.simplebookmarks.service.CollectionService;
import com.appspot.simplebookmarks.service.LastAccessCollectionService;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class AddController extends Controller {

    private String title;
    private String url;
    private String collection;
    private String newCollection;

    private UserService userService;
    private User user;

    private BookmarkService bookmarkService = new BookmarkService();
    private CollectionService collectionService = new CollectionService();
    private LastAccessCollectionService lastAccessCollectionService = new LastAccessCollectionService();

    @Override
    public Navigation run() throws Exception {

        //ユーザー情報取得とログインチェック
        userService = UserServiceFactory.getUserService();
        user = userService.getCurrentUser();
        if (user == null) {
            return redirect(userService.createLoginURL("/bookmark/"));
        }

        //バリデーションチェック
        if(!validate()) return forward("create");

        //パラメーターの値をプロパティに詰め替える
        this.convertParameter();

        //押下したsubmitに対応するメソッドの実行
        if (asString("doAdd") != null) {
            return this.doAdd();
        }

        return redirect("./");
    }

    private Navigation doAdd() {
        //コレクションkeyの取得
        Key collectionKey = null;
        if (!newCollection.equals("")) {
            Collection src = new Collection();
            src.setName(newCollection);
            src.setAuthor(user.getEmail());
            Collection newCollection = collectionService.add(src);
            collectionKey = newCollection.getKey();
        } else {
            collectionKey = KeyFactory.stringToKey(collection);
        }

        //lastAccessCollection
        lastAccessCollectionService.updateLastAccess(user.getEmail(), collectionKey);

        //ブックマーク追加
        Bookmark bookmark = new Bookmark();
        bookmark.setAuthor(user.getEmail());
        bookmark.setTitle(title);
        bookmark.setUrl(url);
        bookmark.setCollectionKey(collectionKey);
        bookmarkService.add(bookmark);
        return redirect(bookmark.getUrl());
    }

    protected boolean validate() {
        Validators v = new Validators(request);
        v.add("t", v.required());
        v.add("u", v.required());
        v.add("c", v.required());
        return v.validate();
    }

    protected void convertParameter() {
        title         = asString("t");
        url           = asString("u");
        collection    = asString("c");
        newCollection = asString("n");
    }
}
